﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonComponent.Common;
using DAL;


public partial class pages_Demo_rowEdit_SingleLineManager : System.Web.UI.Page
{
    /*=======================copy start======================*/
    #region 私有静态变量
    //数据字段
    const string _TABLE_FIELD_PARENT_PKID = "pkid";     //maybe to edit(主表的PKID，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SNAME = "Sname";     //maybe to edit(主表中子表PKID，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SSEX = "Ssex";   //maybe to edit(主要中需要编辑的字段，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SAGE = "Sage";   //maybe to edit(主要中需要编辑的字段，在视图中也应有该字段)
    const string _TABLE_FIELD_PARENT_SOURCE = "Ssource";   //maybe to edit(主要中需要编辑的字段，在视图中也应有该字段)

    //控件名
    const string _CTRL_HIDDENFIELD_PKID = "HiddenField1";   //隐藏表单PK_ID,与aspx页中控件名相对应
    const string _CTRL_LINKBUTTON_EDIT = "lkbtonEdit";      //进行编辑操作的LinkButton,与aspx页中控件名相对应
    const string _CTRL_LINKBUTTON_UPDATE = "lkbtnUpdate";   //进行更改操作的LinkButton,与aspx页中控件名相对应
    const string _CTRL_LABEL_SOURCE = "lblSource";      //进行编辑操作前的Label,与aspx页中控件名相对应
    const string _CTRL_TEXTBOX_SOURCE = "txtSource";      //进行更改操作的TextBox,与aspx页中控件名相对应
    #endregion

    #region 事件
    /// <summary>
    /// 页面载入事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Constants.SESSION_TEMP_DATASET] == null)
        {
            //  maybe to edit(主表的SQL查询字符串)
            string strSQL = "select [pkid],[Sname],[Sage],[Ssex],[Ssource] from [demo_singleLine]";

            //  存放数据
            DataSet ds = RowsEdit.GetDataSet(strSQL);
            
            Session[Constants.SESSION_TEMP_DATASET] = ds;

            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
            ds.Dispose();
        }
    }

    /// <summary>
    /// GiideView 行删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtnDel_Click(object sender, EventArgs e)
    {
        string strPkID = ((LinkButton)sender).CommandArgument;
        //  获取数据
        DataSet ds = (DataSet)Session[Constants.SESSION_TEMP_DATASET];

        string childPkid = RowsEdit.SingleTypeRowDelete(ref ds, _TABLE_FIELD_PARENT_PKID, strPkID,_TABLE_FIELD_PARENT_PKID);
        //  重新放入SESSION
        Session[Constants.SESSION_TEMP_DATASET] = ds;

        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
        ds.Dispose();
    }

    /// <summary>
    /// 行编辑操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtonEdit_Click1(object sender, EventArgs e)
    {
        // maybe to edit(如有多个需要编辑字段，则需要在仿照现有的代码，在aspx页增加相应字段，在aspx.cs仿下列逻辑即可)

        string strIndex = ((LinkButton)sender).CommandArgument;

        LinkButton lkedit = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LINKBUTTON_EDIT) as LinkButton;
        lkedit.Visible = false;
        LinkButton lkeupdate = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LINKBUTTON_UPDATE) as LinkButton;
        lkeupdate.Visible = true;

        Label lbName = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_LABEL_SOURCE) as Label;
        lbName.Visible = false;
        TextBox txName = GridView1.Rows[Convert.ToInt32(strIndex)].FindControl(_CTRL_TEXTBOX_SOURCE) as TextBox;
        txName.Visible = true;
    }

    /// <summary>
    /// 行更改操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lkbtnUpdate_Click(object sender, EventArgs e)
    {
        // maybe to edit(如有多个需要编辑字段，则需要在仿照现有的代码，在aspx页增加相应字段，在aspx.cs仿下列逻辑即可)

        //将超链接"更改"变为"编辑"
        string strIndex = ((LinkButton)sender).CommandArgument;
        int index = Convert.ToInt32(strIndex);
        LinkButton lkedit = GridView1.Rows[index].FindControl(_CTRL_LINKBUTTON_EDIT) as LinkButton;
        lkedit.Visible = true;
        LinkButton lkeupdate = GridView1.Rows[index].FindControl(_CTRL_LINKBUTTON_UPDATE) as LinkButton;
        lkeupdate.Visible = false;

        //对编辑区转换
        Label lblName = GridView1.Rows[index].FindControl(_CTRL_LABEL_SOURCE) as Label;
        lblName.Visible = true;
        TextBox txtTempName = GridView1.Rows[index].FindControl(_CTRL_TEXTBOX_SOURCE) as TextBox;
        txtName.Visible = false;

        //对缓存数据集更新
        HiddenField hfdPkid = GridView1.Rows[index].FindControl(_CTRL_HIDDENFIELD_PKID) as HiddenField;
        string rowName = hfdPkid.Value;
        DataTable dsTable = ((DataSet)Session[Constants.SESSION_TEMP_DATASET]).Tables[0];
        int i = -1;

        foreach (DataRow dr in dsTable.Rows)
        {
            if (dr.RowState != DataRowState.Deleted)
            {
                if (dr[_TABLE_FIELD_PARENT_PKID].ToString() == rowName)
                {
                    i = dsTable.Rows.IndexOf(dr);
                    dsTable.Rows[i][_TABLE_FIELD_PARENT_SOURCE] = txtTempName.Text;
                    break;
                }
            }
        }

        GridView1.DataSource = dsTable.DefaultView;
        GridView1.DataBind();
        dsTable.Dispose();
    }

    /// <summary>
    /// 添加新数据操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnHiddenForUpdate_ServerClick(object sender, EventArgs e)
    {
        //if (Session[Constants.SESSION_NEW_SELECTED] != null && Session[Constants.SESSION_NEW_SELECTED] as string != "")
        //{

        //  maybe to edit(主表的SQL查询字符串,使用时需修改对应字段即可)


        DataTable dsSave = ((DataSet)Session[Constants.SESSION_TEMP_DATASET]).Tables[0];
        DataRow drSave = dsSave.NewRow();

        //获取UUID
        string strPkid = UUIDGenerator.GetUUID();

        drSave[_TABLE_FIELD_PARENT_PKID] = strPkid;
        drSave[_TABLE_FIELD_PARENT_SNAME] = txtName.Text;
        drSave[_TABLE_FIELD_PARENT_SSEX] = txtSex.Text;
        drSave[_TABLE_FIELD_PARENT_SAGE] = txtAge.Text;
        drSave[_TABLE_FIELD_PARENT_SOURCE] = Convert.ToInt32(txtSource.Text);
        dsSave.Rows.Add(drSave);


        GridView1.DataSource = dsSave.DefaultView;
        GridView1.DataBind();
        Session.Remove(Constants.SESSION_NEW_SELECTED);
        //}
    }

    /// <summary>
    /// 数据提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session[Constants.SESSION_TEMP_DATASET];
        int i = 0;  //  记录影响行数

        //  maybe to edit(主表的SQL查询字符串,使用时需修改对应字段即可,如需要加where条件，可在语句中补上即可)
        string strSQL = "select [pkid],[Sname],[Sage],[Ssex],[Ssource] from [demo_singleLine]";
 
        i = RowsEdit.DataSubmit(ds, strSQL);
        Session.Remove(Constants.SESSION_TEMP_DATASET);

        //  maybe to edit(指定跳转页,如要需要，可相应增加跳转逻辑)
        if (i > 0)
        {
            Response.Redirect("SingleResult.html");
        }
    }
    #endregion
    /*=======================copy end======================*/
}

